package com.tsingeye.viid.service.impl;

import cn.hutool.core.collection.CollUtil;
import com.tsingeye.viid.domain.SubImageInfo;
import com.tsingeye.viid.domain.ViidMotorVehicle;
import com.tsingeye.viid.domain.ViidMotorVehicleRequest;
import com.tsingeye.viid.mapper.ViidMotorVehicleImageMapper;
import com.tsingeye.viid.mapper.ViidMotorVehicleMapper;
import com.tsingeye.viid.service.ViidMotorVehicleService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
@Slf4j
public class ViidMotorVehicleServiceImpl implements ViidMotorVehicleService {

    @Autowired
    private ViidMotorVehicleMapper viidMotorVehicleMapper;

    @Autowired
    private ViidMotorVehicleImageMapper viidMotorVehicleImageMapper;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public int batchInsertMotorVehiclesAndMotorVehiclesImages(List<ViidMotorVehicleRequest> motorVehicleObject, List<SubImageInfo> subImageInfoList) {
        // 批量新增
        int motorVehicleInsert = viidMotorVehicleMapper.insertBatch(motorVehicleObject);

        int motorVehicleImageInsert = 0;
        // 判断不为空且机动车添加成功
        if (CollUtil.isNotEmpty(subImageInfoList) && motorVehicleInsert > 0) {
            motorVehicleImageInsert = viidMotorVehicleImageMapper.insertBatch(subImageInfoList);
        }

        return motorVehicleImageInsert;
    }

    @Override
    public List<ViidMotorVehicle> selectViidMotorVehicleList(ViidMotorVehicle viidMotorVehicle) {
        return viidMotorVehicleMapper.selectViidMotorVehicleList(viidMotorVehicle);
    }
}
